<?php
class JobgroupHandler
{
	public static function getGrouplessJobs()
	{
		$oJobs = JobHandler::getJobs();
		$oJobgroups = JobgroupHandler::getJobgroups();
		$iInGroup = array();
		$iOutGroup = array();
		foreach ($oJobgroups as $oThisJobgroup)
		{
			$iJobs = $oThisJobgroup->getJobs();
			foreach ($iJobs as $iThisJob)
			{
				$iInGroup[$iThisJob] = 1;
			}
		}
		foreach ($oJobs as $oThisJob)
		{
			if (!$iInGroup[$oThisJob->iID])
			{
				array_push($iOutGroup, $oThisJob->iID);
			}
		}
		return $iOutGroup;
	}
	
	public static function getJobs($iJobgroup)
	{
		try {
			if (is_array($GLOBALS['jobgroups_jobs'][$iJobgroup]))
			{
				return $GLOBALS['jobgroups_jobs'][$iJobgroup];
			}
			
			$oQuery = DataHandler::query("
				SELECT
					jj.job_id
				FROM
					jobgroups_jobs jj,
					jobs j
				WHERE jj.jobgroup_id = ?
				AND jj.user_id = ?
				AND jj.job_id = j.id
				ORDER BY j.name
			", $iJobgroup, $_SESSION['rid']);
			
			$iJobs = array();
			while (list($iJob) = $oQuery->fetch(PDO::FETCH_NUM)) {
				array_push($iJobs, $iJob);
			}
			
			$GLOBALS['jobgroups_jobs'][$iJobgroup] = $iJobs;
			
			return $GLOBALS['jobgroups_jobs'][$iJobgroup];
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
	
	public static function delJobgroup($iJobgroup)
	{
		try {
			DataHandler::query("
				DELETE FROM jobgroups
				WHERE id = ?
				AND user_id = ?
			", $iJobgroup, $_SESSION['rid']);
			
			DataHandler::query("
				UPDATE jobgroups_jobs
				SET jobgroup_id = '0'
				WHERE jobgroup_id = ?
				AND user_id = ?
			", $iJobgroup, $_SESSION['rid']);
			
			return true;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
	
	public static function changeJobgroup($iJobgroup, $sName)
	{
		try {
			$oCheckQuery = DataHandler::query("
				SELECT id
				FROM jobgroups
				WHERE name = ?
				AND user_id = ?
				AND id != ?
			", $sName, $_SESSION['rid'], $iJobgroup);
			
			if ($oCheckQuery->rowCount() > 0) {
				throw new MiJobException('A job group with that name already exists');
			}
			
			DataHandler::query("
				UPDATE jobgroups
				SET name = ?
				WHERE id = ?
				AND user_id = ?
			", $sName, $iJobgroup, $_SESSION['rid']);
			
			return true;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
	
	public static function addJobgroup($sName)
	{
		try {
			$oCheckQuery = DataHandler::query("
				SELECT id
				FROM jobgroups
				WHERE name = ?
				AND user_id = ?
			", $sName, $_SESSION['rid']);
			
			if ($oCheckQuery->rowCount() > 0) {
				throw new MiJobException('A job group with that name already exist');
			}
			
			DataHandler::query("
				INSERT INTO jobgroups (
					name,
					user_id
				) VALUES (
					?,
					?
				)
			", $sName, $_SESSION['rid']);
			
			$iNewID = DataHandler::lastInsertId();
			
			return $iNewID;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
	
	public static function getJobgroups()
	{
		try {
			if (@is_array($GLOBALS['jobgroups']))
			{
				return $GLOBALS['jobgroups'];
			}

			if (!@$_SESSION['rid']) {
				return array();
			}
			
			$oQuery = DataHandler::query("
				SELECT
					id,
					name
				FROM jobgroups
				WHERE user_id = ?
				ORDER BY name
			", $_SESSION['rid']);
			
			$oJobgroups = array();
			while (list($iID, $sName) = $oQuery->fetch(PDO::FETCH_NUM))
			{
				$oJobgroups[$iID] = new Jobgroup($iID, $sName);
			}

			$GLOBALS['jobgroups'] = $oJobgroups;

			return $GLOBALS['jobgroups'];
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
}
?>
